/*
 * @Description:
 * @Version: 2.0
 * @Autor: xudongxu
 * @Date: 2019-12-07 21:53:20
 * @LastEditors: xudongxu
 * @LastEditTime: 2019-12-08 00:06:18
 */
#ifndef __MYTYPE_H__
#define __MYTYPE_H__
#include "Poco/Dynamic/Pair.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/Dynamic/VarIterator.h"
#include "Poco/JSON/Array.h"
#include "Poco/JSON/Parser.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/Util/LayeredConfiguration.h"
#include "Poco/Util/Util.h"
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
// "1": {
//     "ip": "192.168.1.64",
//     "port": "554",
//     "user": "admin",
//     "passwd": "ad53977301",
//     "rtmpurl": "rtmp://192.168.1.167/live/stream"
// },
using Poco::Dynamic::Pair;
using Poco::Dynamic::Var;
using Poco::JSON::Array;
using Poco::JSON::Object;
using Poco::JSON::Parser;

class CameraInfo {
  public:
    CameraInfo( const std::string& ip, int port, const std::string& user, const std::string& passwd,
                const std::string& rtmpurl, const std::string& comment )
        : ip_( ip ), port_( port ), user_( user ), passwd_( passwd ), rtmpurl_( rtmpurl ), comment_( comment ) {
        std::stringstream ss;
        ss << "rtsp://" << user_ << ":" << passwd_ << "@" << ip_ << ":" << port_ << "/h264/ch1/main/av_stream";
        rtspurl_bgr_ = ss.str();
        std::stringstream ss1;
        ss1 << "rtsp://" << user_ << ":" << passwd_ << "@" << ip_ << ":" << port_ << "/h264/ch33/main/av_stream";
        rtspurl_ir_ = ss1.str();
    }
    // rtsp://admin:ad53937301@192.168.1.64:554/h264/ch1/main/av_stream
    std::string rtspurl_bgr() { return rtspurl_bgr_; }

    std::string rtspurl_ir() { return rtspurl_ir_; }

    std::string comment() { return comment_; }
    std::string rtmpurl() { return rtmpurl_; }

  private:
    std::string ip_;
    int port_;
    std::string user_;
    std::string passwd_;
    std::string rtmpurl_;
    std::string rtspurl_bgr_;
    std::string rtspurl_ir_;
    std::string comment_;
};

std::vector<CameraInfo> cameragroups( Poco::Util::LayeredConfiguration& config ) {
    std::vector<CameraInfo> cameras;
    int num = config.getInt( "counts" );
    for ( int i = 1; i <= num; i++ ) {
        std::string subjson = config.getString( std::to_string( i ) );
        Poco::JSON::Parser parser;
        Poco::Dynamic::Var json = parser.parse( subjson );
        Poco::JSON::Object theObj = *json.extract<Poco::JSON::Object::Ptr>();
        std::string ip = theObj.get( "ip" ).convert<std::string>();
        int port = theObj.get( "port" ).convert<int>();
        std::string user = theObj.get( "user" ).convert<std::string>();
        std::string passwd = theObj.get( "passwd" ).convert<std::string>();
        std::string rtmpurl = theObj.get( "rtmpurl" ).convert<std::string>();
        std::string comment = theObj.get( "comment" ).convert<std::string>();

        CameraInfo info( ip, port, user, passwd, rtmpurl, comment );
        cameras.push_back( info );
    }

    return cameras;
}

#endif